#include <iostream>
#include <memory>
#include <stdlib.h>
#include "protocol.hpp"
#include "httpserver.hpp"
using namespace std;
using namespace Server;
void Usage(string proc)
{
    cout << "Usage:\n\t" << proc << " port\r\n";
}

string suffixtodos(const string &src)
{
    string ret = "Contect-Type: ";
    if (src == ".html")
    {
        ret += "text/html";
    }
    else if (src == ".jpg")
    {
        ret += "application/x-jpg";
    }
    else if (src == ".png")
    {
        ret += "application/x-png";
    }
    ret += "\r\n";
    return ret;
}

// bool Get(const HttpRequest &req, HttpResponse &rep)
// {
//     cout << "-----------------http start-----------------------" << endl;
//     cout << req._inbuffer << endl;
//     cout << "-----------------http end-------------------------" << endl;

//     std::string respline = "HTTP/1.0 200 OK\r\n";
//     std::string respheader = "Context-Type:txt/html\r\n";

//     std::string respblank = "\r\n";
//     std::string body = "<html lang=\"en\"><head><meta charset=\"UTF-8\"><title>for test</title><h1>hello world</h1></head><body><p>第二把都不起 翻了就被翻了呗 第三把都能拉满，再输连败奖励也能叠满。第二局赢了更是血赚。？</p></body></html>\r\n";

//     rep._outbuffer += respline;
//     rep._outbuffer += respheader;
//     rep._outbuffer += respblank;
//     rep._outbuffer += body;

//     return true;
// }

bool Get(const HttpRequest& req,HttpResponse& rep)
{
    cout<<"-----------------http start-----------------------"<<endl;
    cout<<req._inbuffer<<endl;
    cout<<"method: "<<req._method<<endl;
    cout<<"url: "<<req._url<<endl;
    cout<<"httpversion: "<<req._httpversion<<endl;
   cout<<"path: "<<req._path<<endl;
   cout<<"suffix: "<<req._suffix<<endl;
   cout<<"size: "<<req._size<<"字节"<<endl;
    cout<<"-----------------http end-------------------------"<<endl;

    std::string respline = "HTTP/1.0 200 OK\r\n";
     //   std::string respheader = "Context-Type:txt/html\r\n";
    // std::string respline = "HTTP/1.0 307 Temporary Redirect\r\n";//临时重定向307
      std::string respheader = suffixtodos(req._suffix);
    if(req._size>0)
    {
     respheader +="Context-Length";
     respheader +=to_string(req._size);//如果这里給了Context-Length但是没有給实际的size浏览器默认行为是将路径的文件下载下来
    //且在响应报头处没有給正文的长度，网页是无法加载的
    respheader+="\r\n";
    }


    respheader+="Set-Cookie: name=1234567abcdefg; Max-Age=120\r\n";
  //  respheader += "Location: https://www.bilibili.com/\r\n";//重定向到的网址

    std::string respblank = "\r\n";
  //      std::string body = "<html lang=\"en\"><head><meta charset=\"UTF-8\"><title>for test</title><h1>hello world</h1></head><body><p>第二把都不起 翻了就被翻了呗 第三把都能拉满，再输连败奖励也能叠满。第二局赢了更是血赚。？</p></body></html>\r\n";

    //std::string body = "<html lang=\"en\"><head><meta charset=\"UTF-8\"><title>for test</title><h1>hello world</h1></head><body><p>第二把都不起 翻了就被翻了呗 第三把都能拉满，再输连败奖励也能叠满。第二局赢了更是血赚。？</p></body></html>";

    std::string body;
    body.resize(req._size+1);
    if(!Until::readFile(req._path,(char*)body.c_str(),req._size))
    {
        Until::readFile(html_404,(char*)body.c_str(),req._size);
//根据客户端发送来的path字段，对对于的路径读取文件，若所读取的路径不存在文件，那么就将html_404对应的文件填充到body字段中
    }

    rep._outbuffer+=respline;
    rep._outbuffer+=respheader;
    rep._outbuffer+=respblank;
    rep._outbuffer+=body;
    cout<<"---------------http respone start---------------------"<<endl;
  cout<<rep._outbuffer<<endl;
    cout<<"---------------http respone end---------------------"<<endl;
    
    return true;
}

int main(int args, char *argv[])
{
    if (args != 2)
    {
        Usage(argv[0]);
        exit(0);
    }

    uint16_t port = atoi(argv[1]);

    unique_ptr<httpserver> hs(new httpserver(Get, port));

    hs->inithttpserver();
    hs->start();

    return 0;
}